<?php
	define("UPLOAD_ROOTPATH",dirname(__FILE__)."/photos");
	define("CHAPTER_ROOL",10);
	
	require_once (dirname(__FILE__).'/image.inc.php');
	require_once (dirname(__FILE__).'/exifer1_5/ExifInfo.php');
	require_once (dirname(__FILE__).'/database1_0/database.inc.php');
	
class Progress{
	
	const conf_db_type = 'mysql';
	const conf_db_host = 'localhost';// 222.73.218.200 // localhost // mysql241.db.sakura.ne.jp
	const conf_db_user = 'chilcoo';// dantiv // root // chilcoo
	const conf_db_password = 'F0099873a';
	const conf_db_name = 'chilcoo'; // chilcoo
	const conf_db_lang = 'utf8';
	
	public $userId;
	public $database;
	
	function __construct(){
		$this->database = new db(Progress::conf_db_host,Progress::conf_db_user,
			Progress::conf_db_password,Progress::conf_db_name);
		$this->userId = $_POST["userId"];
	}
	
	function upload(){
		// Check the upload
		if (!isset($_FILES["Filedata"]) || !is_uploaded_file
			($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) {
			echo $this->errorMessage(0);
			exit(0);
		}
		// Get the image and create a thumbnail
		$imageStream = imagecreatefromjpeg($_FILES["Filedata"]["tmp_name"]);
		if (!$imageStream) {
			echo $this->errorMessage(1);//$_FILES["Filedata"]["tmp_name"];
			exit(0);
		}
		
		$file_id = md5($_FILES["Filedata"]["tmp_name"] + rand()*100000);
		$path = UPLOAD_ROOTPATH."/$file_id.jpg";
		
		try {
			$sql = "INSERT INTO j70_photos (photoToken, userId, uploadDateTime) 
				VALUES ('$file_id','$this->userId','".date("Y-m-d H:i:s")."');";
			$this->database->insert($sql);
			
			$exifInfo = new ExifInfo($_FILES["Filedata"]["tmp_name"]);
			$sql = "INSERT INTO `j70_exif` (
			  `Make`,   `Model`,   `xResolution`,   
			  `yResolution`,   `ResolutionUnit`, 
			  `Software`,   `DateTime`,   `Artist`,  
			   `YCbCrPositioning`,   `ExifOffset`, 
			  `ExposureTime`,   `FNumber`,  
			   `ExposureProgram`,   `ISOSpeedRatings`, 
			  `ExifVersion`,   `DateTimeOriginal` ,   
			  `ExposureBiasValue`,   `MaxApertureValue`,   
			  `MeteringMode`,   `Flash`,   `FocalLength`, 
			  `ExifImageWidth`,   `ExifImageHeight`,   `photoToken` ) 
			 VALUES('".$exifInfo->getMake()."','".$exifInfo->getModel()."', ".$exifInfo->getXResolution().",
				 ".$exifInfo->getYResolution().", '".$exifInfo->getResolutionUnit()."',
				 '".$exifInfo->getSoftware()."','".$exifInfo->getDateTime()."', '".$exifInfo->getArtist()."',
				 '".$exifInfo->getYCbCrPositioning()."',  '".$exifInfo->getExifOffset()."',
				 ".$exifInfo->getExposureTime().", ".$exifInfo->getFNumber().",
				 '".$exifInfo->getExposureProgram()."', ".$exifInfo->getISOSpeedRatings().",
				 '".$exifInfo->getExifVersion()."', '".$exifInfo->getDateTimeOriginal()."',
				 ".$exifInfo->getExposureBiasValue().", ".$exifInfo->getMaxApertureValue().",
				 '".$exifInfo->getMeteringMode()."', '".$exifInfo->getFlash()."', ".$exifInfo->getFocalLength().",
				 ".$exifInfo->getExifImageWidth().", ".$exifInfo->getExifImageHeight().",'".$file_id."');";
			
			$this->database->insert($sql);
			
		} catch (Exception $e) {
			
		}
		$_image = new image();
		$_image->resize($imageStream, 250, false, $path);
		echo "{\"FILEID\":\"$file_id\"}";	// Return the file id to the script
		
		
	}
	
	function errorMessage($errorCode){
		$str = "";
		switch ($errorCode)
		{
			case 0:
			  $str = "{\"ERROR\",\"invalid upload.\"}";
			  break;
			case 1:
			  $str = "{\"ERROR\",\"could not create image handle.\"}";
			  break;
			case 2:
			  $str = "{\"ERROR\",\"Invalid width or height.\"}";
			  break;
			case 3:
			  $str = "{\"ERROR\",\"Could not fill new image.\"}";
			  break;
			case 4:
			  $str = "{\"ERROR\",\"Could not resize image.\"}";
			  break;
			default:
			  $str = "{\"ERROR\",\"Unknown error.\"}";
		}
		return $str;
	}
	
	function getChapterNumber(){
		// 通过数据ID来识别载入的位置
		$photoId = 137;
		//
		$chapBasic = (int)($photoId / CHAPTER_ROOL);
		$chapPlus = 0;
		if($photoId % CHAPTER_ROOL == 0)
			$chapPlus = 1;
		else
			$chapPlus = 0;
		return ((int)($chapBasic + $chapPlus));
	}
}

$progress = new Progress();
$progress->upload();

?>